﻿
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Net.NetworkInformation;
using System.Web.Script.Serialization;
namespace RsaDemo
{
    public class Common
    {
	private static String APIurl = "http://pay.498.net/api3.0/api.php";

	public static string HttpPost(string postDataStr)  //post读取
	{

		 //发送
		 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(APIurl);
	    request.Timeout = 10000;
	    request.Method = "POST";
	    request.ContentType = "application/x-www-form-urlencoded";

	    try
	    {
		byte[] payload = System.Text.Encoding.UTF8.GetBytes(postDataStr);
		request.GetRequestStream().Write(payload, 0, payload.Length);


		//响应接收
		HttpWebResponse response = (HttpWebResponse)request.GetResponse();

		Stream myResponseStream = response.GetResponseStream();

		StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

		string retString = myStreamReader.ReadToEnd();

		myStreamReader.Close();
		myResponseStream.Close();
		myResponseStream.Dispose();
		request.Abort();

		return retString;
	    }
	    catch
	    {
		request.Abort();
		request = null;
		//Message ms = new Message("网络超时!");
		return "";
	    }

	}


	/// <summary>
	/// MD5加密
	/// </summary>
	/// <returns></returns>
	public static string EncryptionMD5(string str)
	{
	    //MD5加密
	    byte[] result = Encoding.Default.GetBytes(str.Trim());    //tbPass为输入密码的文本框
	    MD5 md5 = new MD5CryptoServiceProvider();
	    byte[] output = md5.ComputeHash(result);
	    return BitConverter.ToString(output).Replace("-", "");
	}

	public static string rsaEncode(String publickey, String str, String business_no)
	{
	    String xmlpublickey = opensslkey.DecodePEMKey(publickey.Replace("-----END PUBLIC KEY-----\n", "-----END PUBLIC KEY-----"));

	    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
	    //rsa.PersistKeyInCsp = false;
	    //rsa.LoadPublicKeyPEM(publickey);
	    byte[] cipherbytes;
	    String rel = "";
	    int len = str.Length;
	    int l = (len % 117) > 0 ? ((len - len % 117) / 117 + 1) : (len / 117);
	    for (int ii = 0; ii < l; ii++)
	    {
		String a = str.Substring(ii * 117, (ii == l - 1) ? len % 117 : 117);
		rsa.FromXmlString(xmlpublickey);
		cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(a), false);
		String tmp = Convert.ToBase64String(cipherbytes);
		rel += tmp;
	    }
	    return UrlEncode(rel + business_no);
	}


	public static string UrlEncode(string str)
	{
	    StringBuilder sb = new StringBuilder();
	    byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
	    for (int i = 0; i < byStr.Length; i++)
	    {
		int a = Convert.ToInt32(byStr[i]);
		if (a > 64 && a < 123)
		{
		    sb.Append(Convert.ToString((char)(byStr[i])));
		}
		else
		{
		    sb.Append(@"%" + Convert.ToString(byStr[i], 16));
		}
	    }

	    return (sb.ToString());
	}

	/// <summary>
	/// 将json数据反序列化为Dictionary
	/// </summary>
	/// <param name="jsonData">json数据</param>
	/// <returns></returns>
	public static Dictionary<string, object> JsonToDictionary(string jsonData)
	{
	    //实例化JavaScriptSerializer类的新实例
	    JavaScriptSerializer jss = new JavaScriptSerializer();
	    try
	    {
		//将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
		return jss.Deserialize<Dictionary<string, object>>(jsonData);
	    }
	    catch (Exception ex)
	    {
		throw new Exception(ex.Message);
	    }
	}
    }
}
